package com.bsh.controller.cloud;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.service.CloudService;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 邀请返利
 * @author Administrator
 *
 */
@Controller
public class ReferralController {
	private static Logger log = LoggerFactory.getLogger(ReferralController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private CloudService cloudServ;

	/**
	 * 邀请返利列表
	 */
	@RequestMapping(value = "/cloud/referral/list")
	public ModelAndView referralList() {
		String userName = request.getParameter("userName") == null ? null : request.getParameter("userName").toString();
		String relUserName = request.getParameter("relUserName") == null ? null : request.getParameter("relUserName").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/cloud/referralList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = cloudServ.getReferralsPage(pager, userName, relUserName);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("userName", userName);
		mv.addObject("relUserName", relUserName);
		return mv;
	}
	
	/**
	 * 邀请返利统计
	 */
	@RequestMapping(value = "/cloud/referral/count")
	public ModelAndView referralCount() {
		String userName = request.getParameter("userName") == null ? null : request.getParameter("userName").toString();
		String count = request.getParameter("count") == null ? null : request.getParameter("count").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/cloud/referralCountList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = cloudServ.getReferralCountPage(pager, userName, count);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("userName", userName);
		mv.addObject("count", count);
		return mv;
	}
	
	
}
